package com.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtils {
	private static FileWriter fw=null;
	private static BufferedWriter bw=null;
	private static PrintWriter pw=null;
	private static String getBinPath(){
		return LogUtils.class.getResource("/").getPath();
	}
	public static void println(String content){
		try {
			openWriter();
			pw.println(content);
			closeWriter();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void printErr(Exception e){
		try {
			printCurrentTime();
			openWriter();
			e.printStackTrace(pw);
			closeWriter();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	public static void printCurrentTime(){
		println(new Timestamp(System.currentTimeMillis()).toString());
	}
	private static void openWriter() throws Exception{
		String fileName=getFilePath();
		fw=new FileWriter(fileName,true);
		bw = new BufferedWriter(fw);
		pw=new PrintWriter(bw);
	}
	public static String getFilePath() throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		return getBinPath()+"log/"+sdf.format(new Date())+".txt";
	}
	private static void closeWriter() throws Exception{
		pw.close();
		bw.close();
		fw.close();
	}
}
